package controller

import (
	"github.com/gin-gonic/contrib/sessions"
	"github.com/gin-gonic/gin"
	uuid "github.com/satori/go.uuid"
	"mynetdisk/model"
	"mynetdisk/utils"
	"net/http"
)

/**
 *@Author tudou
 *@Date 2019/8/24
 **/

/**
###

- [ ] 生成加密文件下载链接(登陆状态)

POST /me/file/password `password` `finalPath`返回的是例如 /download/password/:uuid 且需要输入密码

- [ ] 生成无需密码的分享下载链接(登陆状态)

POST /me/file/share `finalPath` 返回的例如  /download/share/:uuid(你生成的uuid)

- [ ] 生成一次性加密文件下载链接(登录状态)

POST /me/file/password/one `finalPath` `password`

- [ ] 生成一次性分享文件下载(登陆状态)

POST /me/file/share/one `finalPath`
*/

func SharePassword(c *gin.Context) {
	password := c.PostForm("password")
	finalPath := c.PostForm("finalPath")
	time:=c.PostForm("time")
	one:=c.PostForm("one")
	session := sessions.Default(c)
	user := session.Get("user").(uint)
	username := c.PostForm("username")
	//判断该用户是否存在
	var reUser model.User
	if !reUser.GetUserByName(username){
		c.JSON(http.StatusBadRequest,gin.H{"error":"the user is not existed"})
		return
	}
	//判断该文件是否存在
	var f model.File
	rePath,name:=utils.ParseFinalPath(finalPath)
	if !utils.DirExisted(rePath,reUser.Id){
		c.JSON(http.StatusBadRequest, gin.H{"message": "the file dir is not existed"})
		return
	}
	if !utils.FileExisted(rePath,name){
		c.JSON(http.StatusBadRequest, gin.H{"message": "the file is not existed"})
		return
	}
	//再判断是否有权限分享该文件
	if !f.GetFileByFinalPath(name,rePath,reUser.Id){
		c.JSON(http.StatusBadRequest,gin.H{"error":"the file is not existed"})
		return
	}
	if f.Uid!=0{
		if f.Uid!=user{
			c.JSON(http.StatusOK,gin.H{"message":"you don't have the power"})
			return
		}
	}
	num:=1
	if one=="yes"{
		num=0
	}
	uuId, _ := uuid.NewV4()
	var ff = model.FileShare{
		Uid:         f.Id,
		UserId:      reUser.Id,
		Uuid:        uuId,
		Password:    password,
		Timeout:     time,
		DownloadNum: uint(num),
	}
	if !ff.InsertShare(){
		c.JSON(http.StatusOK,gin.H{"error":"create the file failed"})
		return
	}
	url:=c.Request.Host
	url="http://"+url+"/download/password/"+uuId.String()
	c.JSON(http.StatusOK,gin.H{
		"message":"create the link successfully",
		"url": url,
	})
}


func ShareNormal(c *gin.Context){
	finalPath:=c.PostForm("finalPath")
	time:=c.PostForm("time")
	one:=c.PostForm("one")
	username:=c.PostForm("username")
	session := sessions.Default(c)
	userId:=session.Get("user").(uint)
	var reUser model.User
	if !reUser.GetUserByName(username){
		c.JSON(http.StatusBadRequest,gin.H{"error":"the user is not existed"})
		return
	}
	var f model.File
	rePath,name:=utils.ParseFinalPath(finalPath)
	if !utils.DirExisted(rePath,reUser.Id){
		c.JSON(http.StatusBadRequest,gin.H{"error":"the file dir is not exited"})
		return
	}
	if !utils.FileExisted(rePath,name){
		c.JSON(http.StatusBadRequest,gin.H{"error":"the file is not exited"})
		return
	}
	if !f.GetFileByFinalPath(name, rePath,reUser.Id) {
		c.JSON(http.StatusBadRequest, gin.H{"message": "the file is not existed"})
		return
	}
	if f.Uid!=0||f.Uid!=userId{
		c.JSON(http.StatusOK,gin.H{"error":"you don't have the power"})
		return
	}
	num:=1
	if one=="yes"{
		num=0
	}
	uuId, _ := uuid.NewV4()
	var ff =model.FileShare{
		Uid:         f.Id,
		UserId:      f.Uid,
		Uuid:        uuId,
		Password:    "",
		Timeout:     time,
		DownloadNum: uint(num),
	}
	if !ff.InsertShare(){
		c.JSON(http.StatusOK,gin.H{"error":"create the file failed"})
		return
	}
	url:=c.Request.Host
	url="http://"+url+"/download/share/"+uuId.String()
	c.JSON(http.StatusOK,gin.H{
		"message":"create the link successfully",
		"url": url,
	})
}
